package com.ming.service.impl;

import com.ming.dao.DeliveryAddressDao;
import com.ming.po.DeliveryAddress;
import com.ming.po.ResponseBean;
import com.ming.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {

	@Autowired
	private DeliveryAddressDao dad;

	@Override
	public ResponseBean<List<DeliveryAddress>> listDeliveryAddressByUserId(String userId) {
		return new ResponseBean<>(dad.listDeliveryAddressByUserId(userId));
	}
	
	@Override
	@Transactional
	public ResponseBean<Integer> saveDeliveryAddress(DeliveryAddress deliveryAddress) {
		int result = dad.saveDeliveryAddress(deliveryAddress);
		ResponseBean<Integer> rb;
		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"保存失败");
		}
		return rb;
	}
	
	@Override
	public ResponseBean<DeliveryAddress> getDeliveryAddressById(Integer daId) {
		return new ResponseBean<>(dad.getDeliveryAddressById(daId));
	}
	
	@Override
	@Transactional
	public ResponseBean<Integer> updateDeliveryAddress(DeliveryAddress deliveryAddress) {
		int result = dad.updateDeliveryAddress(deliveryAddress);
		ResponseBean<Integer> rb;
		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"修改失败");
		}
		return rb;
	}
	
	@Override
	public ResponseBean<Integer> removeDeliveryAddress(Integer daId) {
		int result = dad.removeDeliveryAddress(daId);
		ResponseBean<Integer> rb;
		if(result!=0){
			rb = new ResponseBean<>(result);
		}else{
			rb = new ResponseBean<>(603,"删除失败");
		}
		return rb;
	}
}
